package team3.jobportal.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import team3.jobportal.businesslogic.Company;
import team3.jobportal.businesslogic.Student;

public class DBManager {
	
	String url= "jdbc:mysql://127.0.0.1:3306/cse6324db";
	Connection con;
	Statement select;
	ResultSet resultSet;
	
	public DBManager()
	{
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection(url, "root", "root");
			select = con.createStatement();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		
		}
		 
		//initialize();
	}
	
	
	
	
	public boolean isStudentLoginValid(Student studentObj)
	{
		boolean loginValid=false;
		try
		{
		if(select!=null)
		{
			String command = "select * from studentlogin where username = \'"+studentObj.getUserName()+"\' and password = \'"+studentObj.getPassword()+"\'";
		    select.executeQuery(command);
		    resultSet = select.getResultSet();
		    if(resultSet.next())
		    {
		    	System.out.println("\n !!! Valid Student login !!!");
		    	loginValid=true;
		    }
		 
		}	
		}
		catch(Exception e)
		{
			loginValid=false;
		}
		return loginValid;
	}
	
	public boolean isCompanyLoginValid(Company companyObj)
	{
		boolean loginValid=false;
		try
		{
		if(select!=null)
		{
			String command = "select * from companylogin where companyusername = \'"+companyObj.getUserName()+"\' and companypassword = \'"+companyObj.getPassword()+"\'";
		    select.executeQuery(command);
		    resultSet = select.getResultSet();
		    if(resultSet.next())
		    {
		    	System.out.println("\n !!! Valid Company login !!!");
		    	loginValid=true;
		    }
		 
		}	
		}
		catch(Exception e)
		{
			loginValid=false;
		}
		return loginValid;
	}
	
	public boolean createStudentAccount(Student studentObj)
	{
		boolean result= false;
		System.out.println("In DB create student account");
		try {
			if(select!=null)
			{
				String command = "select * from studentdetails where std_email = \'"+studentObj.getEmailId()+"\'";
			    select.executeQuery(command);
			    resultSet = select.getResultSet();
			    resultSet.first();
			    int id = resultSet.getInt("std_id");
			    System.out.println("Student id is "+id);
			    command = "insert into studentlogin (std_login_id, username,password,security_qn, security_ans) values("+id+",'"+studentObj.getEmailId()+"','"+studentObj.getPassword()+"','"+studentObj.getSecQn()+"','"+studentObj.getSecAns()+"')";
				select.executeUpdate(command);
			    result = true;
			}	
			
		} 
		catch (Exception e) 
		{	
			System.out.println("Error!!");
			result=false;
		   e.printStackTrace();
		}
		System.out.println("In DB create student account returning "+result);
		return result;
	}
/*	public boolean insertStudentUserName(String uName)
	{
		try {
			if(select!=null)
			{
				String cmd = "insert into test values(100,\'"+uName+"\')";
				//cmd = "insert into test values(100,\'Sabarish\')";
				//select.executeUpdate(cmd);
			}	else
				System.out.println("select is null");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}
	
	
	*/
	
}
